# NOT RUN {
x <- xts(1:3, Sys.Date()+1:3)
xx <- cbind(x,x)
# drop=FALSE for xts, differs from zoo and matrix
z <- as.zoo(xx)
z/z[,1]
m <- as.matrix(xx)
m/m[,1]
# this will fail with non-conformable arrays (both retain dim)
tryCatch(
xx/x[,1],
error=function(e) print("need to set drop=TRUE")
)
# correct way
xx/xx[,1,drop=TRUE]
# or less efficiently
xx/drop(xx[,1])
# likewise
xx/coredata(xx)[,1]
x <- xts(1:1000, as.Date("2000-01-01")+1:1000)
y <- xts(1:1000, as.POSIXct(format(as.Date("2000-01-01")+1:1000)))
x.subset <- index(x)[1:20]
x[x.subset] # by original index type
system.time(x[x.subset])
x[as.character(x.subset)] # by character string. Beware!
system.time(x[as.character(x.subset)]) # slow!
system.time(x[I(as.character(x.subset))]) # wrapped with I(), faster!
x['200001'] # January 2000
x['1999/2000'] # All of 2000 (note there is no need to use the exact start)
x['1999/200001'] # January 2000
x['2000/200005'] # 2000-01 to 2000-05
x['2000/2000-04-01'] # through April 01, 2000
y['2000/2000-04-01'] # through April 01, 2000 (using POSIXct series)
# }
Run the code above in your browser using DataLab